package com.ruoyi.common.utils.poi;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PdfUtil {
    /**
     * 将上传的word文档保存到本地
     * @param file
     * @return
     * @throws IOException
     */
    public static String save(MultipartFile file) throws IOException {
        String path= FileUploadUtils.upload(file);
        String realPath=path.substring(path.indexOf("e")+2);
        String baseDir= RuoYiConfig.getProfile();
        String filePath=baseDir+realPath;
        return filePath;
    }
    public static String getContent(String path){
        //声明一个文件对象，指向将要导入的pdf文件
        File pdfFile=new File(path);
        //声明一个pdfDocument对象
        PDDocument document=null;
        try{
            //从文件中加载pdf文档
            document=PDDocument.load(pdfFile);
            //使用PDFTextStripper提取文本
            PDFTextStripper stripper=new PDFTextStripper();
            String content=stripper.getText(document);
            return content;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //确保在完成后关闭文档
            try {
                if(document!=null){
                    document.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return path;
    }

    //数据截取
    public static Map<String,String> setResume(String content){
        Map<String,String> map=new HashMap<>();
        String skillRegex="专业技能< < <\\s*(.*?)(?=\\n工作经验< < <)";
        String experienceRegex="工作经验< < <\\s*(.*?)(?=\\n自我评价< < <)";
        String educationRegex="< < < 个人信息\\s*(.*?)(?=< < < 教育背景)";
        String skill=regex(skillRegex,content);
        String experience=regexExperience(experienceRegex,content);
        String education=regexEducation(educationRegex,content);
        map.put("skills",skill);
        map.put("experience",experience);
        map.put("education",education);
        return map;
    }

    public static String regex(String regex,String content){
        Pattern pattern=Pattern.compile(regex,Pattern.DOTALL | Pattern.MULTILINE);
        Matcher matcher=pattern.matcher(content);
        if(matcher.find()){
            String data=matcher.group(1);
            return data;
        }
        return null;
    }
    public static String regexEducation(String regex,String content){
        Pattern pattern=Pattern.compile(regex,Pattern.DOTALL | Pattern.MULTILINE);
        Matcher matcher=pattern.matcher(content);
        if(matcher.find()){
            String data=matcher.group(1).trim();
            return data;
        }
        return null;
    }
    public static String regexExperience(String regex,String content){
        Pattern pattern=Pattern.compile(regex,Pattern.DOTALL | Pattern.MULTILINE);
        Matcher matcher=pattern.matcher(content);
        if(matcher.find()){
            String data=matcher.group(1).replaceAll("\\s", "");
            return data;
        }
        return null;
    }
}
